package com.fjnu.service.impl;

import com.fjnu.dao.UserDao;
import com.fjnu.dto.UserConditionDto;
import com.fjnu.dto.UserDto;
import com.fjnu.entity.User;
import com.fjnu.service.UserService;
import com.fjnu.uitl.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User queryByName(String name) {
      return  userDao.selectByCondition(new UserConditionDto(null, name, null, null, null, null)).get(0);
    }

    @Override
    public int register(User user) {
        return userDao.insert(user)>0?1:0;
    }

    @Override
    public PageResult<User> queryUsers(UserConditionDto userConditionDto, Integer pageNum, Integer pageSize) {
        return new PageResult<>(userDao.selectByConditionWithLimit(userConditionDto,
                (pageNum-1)*pageSize, pageSize).size(),
                userDao.selectByConditionWithLimit(userConditionDto,
                        (pageNum-1)*pageSize, pageSize));
    }

    @Override
    public void addAssignRole(Integer userId, List<Integer> roleIds) {
        userDao.batchInsertUserRoles(userId, roleIds);
    }

    @Override
    public List<User> list(UserConditionDto userConditionDto) {
        return userDao.selectByCondition(userConditionDto);
    }

    @Override
    public int updatePassword(Integer id, String password) {
        return userDao.updatePassword(id, password);
    }

    @Override
    public int add(UserDto userDto) {
        User user = new User();
        user.setName(userDto.getName());
        user.setPassword(userDto.getPassword());
        user.setEmail(userDto.getEmail());
        user.setGender(userDto.getGender());
        user.setStatus(1);
        user.setScore(0);
        user.setCreateTime(new Date(System.currentTimeMillis()));
        return userDao.insert(user)>0?1:0;
    }

}
